﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ManagementTool.ESB;

namespace ManagementTool
{
    public partial class ServiceInfoWindow : ChildWindow
    {
        private readonly ExposeServiceBusClient exposeServiceBusClient;
        private ESB.Service service; 
        public ESB.Service Service
        {
            get { return service; }
            set { service = value; AssignValues(); }
        }

        public ServiceInfoWindow(ESB.Service service, ESB.ExposeServiceBusClient exposeServiceBusClient)
        {
            this.exposeServiceBusClient = exposeServiceBusClient;
            InitializeComponent();
            this.Service = service;
        }

        void AssignValues()
        {
            this.Title = service.Name;
            uxServiceName.Text = service.Name;
            uxIsServiceRouted.IsChecked = service.IsRouted;
            uxServicePath.Text = service.ServicePath.ToString();
            uxMetadataPath.Text = service.MetadataPath.ToString();
            uxRoutingServicePath.Text = service.RoutingServicePath.ToString();
            uxRoutingMetadataPath.Text = service.RoutingMetadataPath.ToString();
            uxWsdlLink.NavigateUri = new Uri(service.ServicePath+"?wsdl");
        }

        private void uxReload_Click(object sender, RoutedEventArgs e)
        {
            exposeServiceBusClient.GetServiceCompleted += new EventHandler<GetServiceCompletedEventArgs>(exposeServiceBusClient_GetServiceCompleted);
            exposeServiceBusClient.GetServiceAsync(service.ServicePath);
        }

        void exposeServiceBusClient_GetServiceCompleted(object sender, GetServiceCompletedEventArgs e)
        {
            exposeServiceBusClient.GetServiceCompleted -= exposeServiceBusClient_GetServiceCompleted;
            Service = e.Result;
        }

        private void uxSave_Click(object sender, RoutedEventArgs e)
        {
            if (service.IsRouted != uxIsServiceRouted.IsChecked)
                if (uxIsServiceRouted.IsChecked.Value)
                    exposeServiceBusClient.RouteServiceAsync(service);
                else
                    exposeServiceBusClient.UnRouteServiceAsync(service);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            exposeServiceBusClient.ChangeRouteCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(exposeServiceBusClient_ChangeRouteCompleted);
            exposeServiceBusClient.ChangeRouteAsync(service,uxChangePath.Text);
        }

        void exposeServiceBusClient_ChangeRouteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            exposeServiceBusClient.ChangeRouteCompleted -= exposeServiceBusClient_ChangeRouteCompleted;
            uxReload_Click(null,null);
        }
    }
}

